#!/bin/bash

SUDO="sudo "
if [[ $(id -u) -eq 0 ]]; then SUDO=""; fi

$SUDO apt install iptables -y

$SUDO rm -rf ./docker-tools
git clone https://gitee.com/wyanlord/docker-tools.git
cd docker-tools

if [[ ! -x "/usr/bin/docker-compose" ]]; then
    $SUDO tar -zxvf docker-compose-linux-x86_64.tar.gz
    $SUDO mv docker-compose-linux-x86_64 /usr/bin/docker-compose
    $SUDO chmod a+x /usr/bin/docker-compose
fi

if [[ ! -x "/usr/bin/kubectl" ]]; then
    $SUDO tar -zxvf kubectl.tar.gz -C /usr/bin --strip-components=1
fi

if [[ ! -x "/usr/bin/helm" ]]; then
    tar zxvf helm-v3.10.1-linux-amd64.tar.gz --strip-components=1 linux-amd64/helm
    $SUDO mv helm /usr/bin/helm
fi

if [[ ! -x "/usr/bin/k9s" ]]; then
    tar zxvf k9s_Linux_amd64.tar.gz k9s
    $SUDO mv k9s /usr/bin/k9s
fi

if [[ ! -x "/usr/bin/docker" ]]; then
    $SUDO tar -zxvf docker-25.0.3.tgz -C /usr/bin --strip-components=1
    cat << EOF | $SUDO tee /etc/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target
Wants=network-online.target

[Service]
Type=notify
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP \$MAINPID
TimeoutStartSec=0
RestartSec=2
Restart=always
StartLimitBurst=3
StartLimitInterval=60s
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
Delegate=yes
KillMode=process
OOMScoreAdjust=-500

[Install]
WantedBy=multi-user.target
EOF

    $SUDO mkdir -p /etc/docker
    cat << EOF | $SUDO tee /etc/docker/daemon.json
{
  "registry-mirrors": ["https://ohbxz313.mirror.aliyuncs.com"],
  "insecure-registries": []
}
EOF
fi

rm -rf ../docker-tools

echo "start docker server, you need to execute below:"
echo -e "\t$SUDO systemctl start docker"
echo -e "\t$SUDO groupadd docker"
echo -e "\t$SUDO gpasswd -a \$USER docker"
echo -e "\t$SUDO systemctl restart docker"
echo -e "\t$SUDO chmod a+rw /var/run/docker.sock"
